﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using DBUtlity;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class WorkflowService
    {
        /// <summary>
        /// 查找用户可以创建的工作流
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<UserTeamRole> GetUserCreateWorkflow(string userName)
        {
            string sql = "select UserName,TeamId,RoleId,WorkMouldId,WorkName from UserTeamRoles ";
            sql += "inner join WorkflowMoulds on WorkflowMoulds.WorkParentId=UserTeamRoles.WorkMouldId ";
            sql += "where UserName=@UserName and RoleId=2";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserName",userName)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<UserTeamRole> userList = new List<UserTeamRole>();
            while (objReader.Read())
            {
                userList.Add(new UserTeamRole
                {
                    UserName = objReader["UserName"].ToString(),
                    TeamId = Convert.ToInt32(objReader["TeamId"]),
                    RoleId = Convert.ToInt32(objReader["RoleId"]),
                    WorkMouldId = Convert.ToInt32(objReader["WorkMouldId"]),
                    WorkName = objReader["WorkName"].ToString()
                });

            }
            objReader.Close();
            return userList;
        }


        /// <summary>
        /// 根据ID删除主表信息
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public int DeleteWorkflowStatus(int workParentId)
        {
            string sql = "delete from WorkflowStatuses where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        #region 查询模板
        /// <summary>
        /// 根据ID查找模板主表信息
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public WorkflowMould GetParentWorkflowMould(int workParentId)
        {
            string sql = "select WorkParentId,WorkName,NodeCount,TableName,ReaderTeamId,ReaderRoleId,WebName ";
            sql += "from WorkflowMoulds ";
            sql += "where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            WorkflowMould Workflow = null;
            if (objReader.Read())
            {
                Workflow = new WorkflowMould()
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    WorkName = objReader["WorkName"].ToString(),
                    NodeCount = Convert.ToInt32(objReader["NodeCount"]),
                    TableName = objReader["TableName"].ToString(),
                    ReaderTeamId = Convert.ToInt32(objReader["ReaderTeamId"]),
                    ReaderRoleId = Convert.ToInt32(objReader["ReaderRoleId"]),
                    WebName = objReader["WebName"].ToString()
                };
            }
            objReader.Close();
            return Workflow;
        }
        /// <summary>
        /// 根据主表ID查找模板子表信息
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public List<WorkflowChildMould> GetChildWorkflowMould(int workParentId)
        {
            string sql = "select WorkChildId,WorkParentId,NodeNo,IsSingleNode,";
            sql += "ChildNodeNo,TeamId,RoleId,NodeName ";
            sql += "from WorkflowChildMoulds ";
            sql += "where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<WorkflowChildMould> workList = new List<WorkflowChildMould>();
            while (objReader.Read())
            {
                workList.Add(new WorkflowChildMould()
                {
                    WorkChildId = Convert.ToInt32(objReader["WOrkChildId"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    NodeNo = Convert.ToInt32(objReader["NodeNo"]),
                    IsSingleNode = Convert.ToInt32(objReader["IsSingleNode"]),
                    ChildNodeNo = Convert.ToInt32(objReader["ChildNodeNo"]),
                    TeamId = Convert.ToInt32(objReader["TeamId"]),
                    RoleId = Convert.ToInt32(objReader["RoleId"]),
                    NodeName = objReader["NodeName"].ToString()
                });
            }
            objReader.Close();
            return workList;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据创建者用户名、状态查询工作流主表
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="statusDefine"></param>
        /// <returns></returns>
        public List<WorkflowStatus> GetWorkflowStatus(string userName, int statusDefine)
        {
            string sql = "select WorkParentId,CreaterUserName,WorkMouldId,WorkName,NodeCount,StatusDefine,";
            sql += "StatusNo,CreateTime,CompleteTime,CreaterTeamId,CreaterRoleId,";
            sql += "ReaderTeamId,ReaderRoleId,TableName,WebName,Critical,Content,ParentReturnReason from WorkflowStatuses ";
            sql += "where CreaterUserName=@CreaterUserName and StatusDefine=@StatusDefine";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@CreaterUserName",userName),
                new SqlParameter("@StatusDefine",statusDefine)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<WorkflowStatus> list = new List<WorkflowStatus>();
            while (objReader.Read())
            {
                list.Add(new WorkflowStatus
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    CreaterUserName = objReader["CreaterUserName"].ToString(),
                    WorkMouldId = Convert.ToInt32(objReader["WorkMouldId"]),
                    WorkName = objReader["WorkName"].ToString(),
                    NodeCount = Convert.ToInt32(objReader["NodeCount"]),
                    StatusDefine = Convert.ToInt32(objReader["StatusDefine"]),
                    StatusNo = Convert.ToInt32(objReader["StatusNo"]),
                    CreateTime = Convert.ToDateTime(objReader["CreateTime"]),
                    CompleteTime = objReader["CompleteTime"] is DBNull ? Convert.ToDateTime("0001-01-01 0:00:00") : Convert.ToDateTime(objReader["CompleteTime"]),
                    CreaterTeamId = Convert.ToInt32(objReader["CreaterTeamId"]),
                    CreaterRoleId = Convert.ToInt32(objReader["CreaterRoleId"]),
                    ReaderTeamId = Convert.ToInt32(objReader["ReaderTeamId"]),
                    ReaderRoleId = Convert.ToInt32(objReader["ReaderRoleId"]),
                    TableName = objReader["TableName"].ToString(),
                    WebName = objReader["Webname"].ToString(),
                    Critical = objReader["Critical"].ToString(),
                    Content = objReader["Content"].ToString(),
                    ParentReturnReason = objReader["ParentReturnReason"].ToString()
                });
            }
            objReader.Close();
            return list;
        }
        /// <summary>
        /// 根据id查找工作流
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public WorkflowStatus GetWorkflowStatus(int workParentId)
        {
            string sql = "select WorkParentId,CreaterUserName,WorkMouldId,WorkName,NodeCount,StatusDefine,";
            sql += "StatusNo,CreateTime,CompleteTime,CreaterTeamId,CreaterRoleId,";
            sql += "ReaderTeamId,ReaderRoleId,TableName,WebName,Critical,Content from WorkflowStatuses ";
            sql += "where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            WorkflowStatus objWorkflow = null;
            while (objReader.Read())
            {
                objWorkflow = new WorkflowStatus()
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    CreaterUserName = objReader["CreaterUserName"].ToString(),
                    WorkMouldId = Convert.ToInt32(objReader["WorkMouldId"]),
                    WorkName = objReader["WorkName"].ToString(),
                    NodeCount = Convert.ToInt32(objReader["NodeCount"]),
                    StatusDefine = Convert.ToInt32(objReader["StatusDefine"]),
                    StatusNo = Convert.ToInt32(objReader["StatusNo"]),
                    CreateTime = Convert.ToDateTime(objReader["CreateTime"]),
                    CompleteTime = objReader["CompleteTime"] is DBNull ? Convert.ToDateTime("0001-01-01 0:00:00") : Convert.ToDateTime(objReader["CompleteTime"]),
                    CreaterTeamId = Convert.ToInt32(objReader["CreaterTeamId"]),
                    CreaterRoleId = Convert.ToInt32(objReader["CreaterRoleId"]),
                    ReaderTeamId = Convert.ToInt32(objReader["ReaderTeamId"]),
                    ReaderRoleId = Convert.ToInt32(objReader["ReaderRoleId"]),
                    TableName = objReader["TableName"].ToString(),
                    WebName = objReader["Webname"].ToString(),
                    Critical = objReader["Critical"].ToString(),
                    Content = objReader["Content"].ToString()
                };
            }
            objReader.Close();
            return objWorkflow;
        }


        /// <summary>
        /// 根据状态主表id和节点号查询子表
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="nodeNo"></param>
        /// <returns></returns>
        public List<WorkflowChildStatus> GetWorkflowChildStatus(int workParentId, int nodeNo)
        {
            string sql = "select WorkChildId,WorkParentId,NodeNo,IsSingleNode,ChildNodeNo,";
            sql += "TeamId,RoleId,StatusNo,LastChangeTime,UserName,NodeName,WorkMouldId from WorkflowChildStatuses ";
            sql += "where WorkParentId=@WorkParentId and NodeNo=@NodeNo";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@NodeNo",nodeNo)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<WorkflowChildStatus> list = new List<WorkflowChildStatus>();
            while (objReader.Read())
            {

                list.Add(new WorkflowChildStatus()
                {
                    WorkChildId = Convert.ToInt32(objReader["WorkChildId"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    NodeNo = Convert.ToInt32(objReader["NodeNo"]),
                    IsSingleNode = Convert.ToInt32(objReader["IsSingleNode"]),
                    ChildNodeNo = Convert.ToInt32(objReader["ChildNodeNo"]),
                    TeamId = Convert.ToInt32(objReader["TeamId"]),
                    RoleId = Convert.ToInt32(objReader["RoleId"]),
                    StatusNo = Convert.ToInt32(objReader["StatusNo"]),
                    LastChangeTime = Convert.ToDateTime(objReader["LastChangeTime"]),
                    UserName = objReader["UserName"] is DBNull ? "" : objReader["UserName"].ToString(),
                    NodeName = objReader["NodeName"].ToString(),
                    WorkMouldId = Convert.ToInt32(objReader["WorkMouldId"])
                });
            }
            objReader.Close();
            return list;
        }
        /// <summary>
        /// 根据状态主表id和节点号、子节点序号查询子节点
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="nodeNo"></param>
        /// <returns></returns>
        public WorkflowChildStatus GetWorkflowChildStatus(int workParentId, int nodeNo, int childNodeNo)
        {
            string sql = "select WorkChildId,WorkParentId,NodeNo,IsSingleNode,ChildNodeNo,";
            sql += "TeamId,RoleId,StatusNo,LastChangeTime,UserName,NodeName,WorkMouldId from WorkflowChildStatuses ";
            sql += "where WorkParentId=@WorkParentId and NodeNo=@NodeNo and ChildNodeNo=@ChildNodeNo";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@NodeNo",nodeNo),
                new SqlParameter("@ChildNodeNo",childNodeNo)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            WorkflowChildStatus workflowChild = null;
            if (objReader.Read())
            {

                workflowChild = new WorkflowChildStatus()
                {
                    WorkChildId = Convert.ToInt32(objReader["WorkChildId"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    NodeNo = Convert.ToInt32(objReader["NodeNo"]),
                    IsSingleNode = Convert.ToInt32(objReader["IsSingleNode"]),
                    ChildNodeNo = Convert.ToInt32(objReader["ChildNodeNo"]),
                    TeamId = Convert.ToInt32(objReader["TeamId"]),
                    RoleId = Convert.ToInt32(objReader["RoleId"]),
                    StatusNo = Convert.ToInt32(objReader["StatusNo"]),
                    LastChangeTime = Convert.ToDateTime(objReader["LastChangeTime"]),
                    UserName= objReader["UserName"].ToString(),
                    NodeName = objReader["NodeName"].ToString(),
                    WorkMouldId = Convert.ToInt32(objReader["WorkMouldId"])
                };
            }
            objReader.Close();
            return workflowChild;
        }
        /// <summary>
        /// 根据子表ID 查找子节点
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="nodeNo"></param>
        /// <returns></returns>
        public WorkflowChildStatus GetWorkflowChildStatus(int workChildId)
        {
            string sql = "select WorkChildId,WorkParentId,NodeNo,IsSingleNode,ChildNodeNo,";
            sql += "TeamId,RoleId,StatusNo,LastChangeTime,UserName,NodeName,WorkMouldId from WorkflowChildStatuses ";
            sql += "where WorkChildId=@WorkChildId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkChildId",workChildId)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            WorkflowChildStatus workflowChild = null;
            if (objReader.Read())
            {

                workflowChild = new WorkflowChildStatus()
                {
                    WorkChildId = Convert.ToInt32(objReader["WorkChildId"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    NodeNo = Convert.ToInt32(objReader["NodeNo"]),
                    IsSingleNode = Convert.ToInt32(objReader["IsSingleNode"]),
                    ChildNodeNo = Convert.ToInt32(objReader["ChildNodeNo"]),
                    TeamId = Convert.ToInt32(objReader["TeamId"]),
                    RoleId = Convert.ToInt32(objReader["RoleId"]),
                    StatusNo = Convert.ToInt32(objReader["StatusNo"]),
                    LastChangeTime = Convert.ToDateTime(objReader["LastChangeTime"]),
                    UserName = objReader["UserName"] is DBNull ? "" : objReader["UserName"].ToString(),
                    NodeName = objReader["NodeName"].ToString(),
                    WorkMouldId = Convert.ToInt32(objReader["WorkMouldId"])
                };
            }
            objReader.Close();
            return workflowChild;
        }
        /// <summary>
        /// 根据用户名查找需要审批的工作流
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<WorkflowStatus> GetWorkflowStatus(string userName)
        {
            string sql = "select WorkflowStatuses.WorkParentId,CreaterUserName,";
            sql += "WorkflowStatuses.WorkMouldId,WorkName,NodeCount,StatusDefine,";
            sql += "WorkflowStatuses.StatusNo,CreateTime,CompleteTime,CreaterTeamId,";
            sql += "CreaterRoleId,ReaderTeamId,ReaderRoleId,TableName,WebName,ChildNodeNo,";
            sql += "WorkflowChildStatuses.StatusNo,IsSingleNode,WorkChildId,NodeName,Critical,";
            sql += "Content,IsRead,DisplayName,ReturnReason from WorkflowStatuses inner join WorkflowChildStatuses ";
            sql += "on WorkflowStatuses.WorkParentId=WorkflowChildStatuses.WorkParentId ";
            sql += "inner join UserInfoes on WorkflowStatuses.CreaterUserName=UserInfoes.UserName ";
            sql += "where WorkflowChildStatuses.UserName=@UserName";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserName",userName)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<WorkflowStatus> list = new List<WorkflowStatus>();
            while (objReader.Read())
            {
                list.Add(new WorkflowStatus()
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    CreaterUserName = objReader["CreaterUserName"].ToString(),
                    WorkMouldId = Convert.ToInt32(objReader["WorkMouldId"]),
                    WorkName = objReader["WorkName"].ToString(),
                    NodeCount = Convert.ToInt32(objReader["NodeCount"]),
                    StatusDefine = Convert.ToInt32(objReader["StatusDefine"]),
                    StatusNo = Convert.ToInt32(objReader[6]),
                    CreateTime = Convert.ToDateTime(objReader["CreateTime"]),
                    CompleteTime = objReader["CompleteTime"] is DBNull ? Convert.ToDateTime("0001-01-01 0:00:00") : Convert.ToDateTime(objReader["CompleteTime"]),
                    CreaterTeamId = Convert.ToInt32(objReader["CreaterTeamId"]),
                    CreaterRoleId = Convert.ToInt32(objReader["CreaterRoleId"]),
                    ReaderTeamId = Convert.ToInt32(objReader["ReaderTeamId"]),
                    ReaderRoleId = Convert.ToInt32(objReader["ReaderRoleId"]),
                    TableName = objReader["TableName"].ToString(),
                    WebName = objReader["Webname"].ToString(),
                    ChildNodeNo = Convert.ToInt32(objReader["ChildNodeNo"]),
                    ChildStatusNo = Convert.ToInt32(objReader[16]),
                    IsSingleNode = Convert.ToInt32(objReader["IsSingleNode"]),
                    WorkChildId = Convert.ToInt32(objReader["WorkChildId"]),
                    NodeName = objReader["NodeName"].ToString(),
                    Critical = objReader["Critical"].ToString(),
                    Content = objReader["Content"].ToString(),
                    IsRead = Convert.ToInt32(objReader["IsRead"]),
                    CreaterDisplayName = objReader["DisplayName"].ToString(),
                    ReturnReason = objReader["ReturnReason"].ToString()
                });
            }
            objReader.Close();
            return list;
        }
        /// <summary>
        /// 查找点击暂停用户的姓名
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public List<string> GetPauseName(int workParentId)
        {
            string sql = "select DisplayName from UserInfoes";
            sql += " inner join WorkflowChildStatuses on WorkflowChildStatuses.UserName= UserInfoes.UserName";
            sql += " where WorkParentId=@WorkParentId and StatusNo=@StatusNo";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId", workParentId),
                new SqlParameter("@StatusNo", -1)
            };
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<string> list = new List<string>();
            while(objReader.Read())
            {
                list.Add(objReader["DisplayName"].ToString());
            }
            return list;
        }
        #endregion

        #region 新建工作流
        /// <summary>
        /// 插入工作流状态主表数据 返回id值
        /// </summary>
        /// <param name="workMould"></param>
        /// <param name="userName"></param>
        /// <param name="CreaterTeamId"></param>
        /// <returns>ID</returns>
        public int InsertWorkflowStatuses(WorkflowMould workMould, string userName, int CreaterTeamId, string critical, string content)
        {
            string sql = "insert into WorkflowStatuses(CreaterUserName,";
            sql += "WorkMouldId,WorkName,NodeCount,CreaterTeamId,CreaterRoleId,";
            sql += "ReaderTeamId,ReaderRoleId,TableName,WebName,Critical,Content) values";
            sql += "(@CreaterUserName,@WorkMouldId,@WorkName,@NodeCount,@CreaterTeamId,";
            sql += "@CreaterRoleId,@ReaderTeamId,@ReaderRoleId,@TableName,@WebName,@Critical,@Content) ";
            sql += "select @@identity as WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@CreaterUserName",userName),
                new SqlParameter("@WorkMouldId",workMould.WorkParentId),
                new SqlParameter("@WorkName",workMould.WorkName),
                new SqlParameter("@NodeCount",workMould.NodeCount),
                new SqlParameter("@CreaterTeamId",workMould.CreateTeamId),
                new SqlParameter("@CreaterRoleId",workMould.CreateRoleId),
                new SqlParameter("@ReaderTeamId",workMould.ReaderTeamId),
                new SqlParameter("@ReaderRoleId",workMould.ReaderRoleId),
                new SqlParameter("@TableName",workMould.TableName),
                new SqlParameter("@WebName",workMould.WebName),
                new SqlParameter("@Critical",critical),
                new SqlParameter("@Content",content)
            };
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql, param));
        }
        /// <summary>
        /// 插入工作流状态子表数据
        /// </summary>
        /// <param name="workChildMould"></param>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public bool InsertWorkflowChildStatus(List<WorkflowChildMould> workChildMould, int workParentId, int CreaterTeamId)
        {
            string sql = "insert into WorkflowChildStatuses ";
            sql += "(WorkParentId,NodeNo,IsSingleNode,ChildNodeNo,TeamId,RoleId,LastChangeTime,NodeName,WorkMouldId) ";
            sql += "values(@WorkParentId,@NodeNo,@IsSingleNode,@ChildNodeNo,@TeamId,@RoleId,@LastChangeTime,@NodeName,@WorkMouldId)";
            List<SqlParameter[]> detailParam = new List<SqlParameter[]>();
            foreach (var item in workChildMould)
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@WorkParentId",workParentId),
                    new SqlParameter("@NodeNo",item.NodeNo),
                    new SqlParameter("@IsSingleNode",item.IsSingleNode),
                    new SqlParameter("@ChildNodeNo",item.ChildNodeNo),
                    new SqlParameter("@TeamId",item.TeamId==0?CreaterTeamId:item.TeamId),
                    new SqlParameter("@RoleId",item.RoleId),
                    new SqlParameter("@LastChangeTime",DateTime.Now),
                    new SqlParameter("@NodeName",item.NodeName),
                    new SqlParameter("@WorkMouldId",item.WorkParentId)
                };
                detailParam.Add(param);
            }
            return SQLHelper.UpdateByTran(null, null, sql, detailParam);
        }
        #endregion

        #region 更新主表数据
        /// <summary>
        /// 需要参数：主表ID
        /// 更新参数：主表状态（StatusDefine和StatusNo）
        /// </summary>
        /// <param name="workflowParentId"></param>
        /// <param name="statusDefine"></param>
        /// <param name="statusNo"></param>
        /// <returns></returns>
        public int UpdateWorkflowStatus(int workflowParentId, int statusDefine, int statusNo)
        {
            string sql;
            if (statusDefine == 100)
            {
                sql = "update WorkflowStatuses set StatusDefine=@StatusDefine,";
                sql += "StatusNo=@StatusNo,CompleteTime=getdate() where WorkParentId=@WorkParentId";
            }
            else
            {
                sql = "update WorkflowStatuses set StatusDefine=@StatusDefine,";
                sql += "StatusNo=@StatusNo where WorkParentId=@WorkParentId";
            }
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@StatusDefine",statusDefine),
                new SqlParameter("@StatusNo",statusNo),
                new SqlParameter("@WorkParentId",workflowParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 需要参数：主表ID
        /// 更新参数：主表状态（只有StatusDefine）
        /// </summary>
        /// <param name="workflowParentId"></param>
        /// <param name="statusDefine"></param>
        /// <param name="statusNo"></param>
        /// <returns></returns>
        public int UpdateWorkflowStatus(int workflowParentId, int statusDefine)
        {
            string sql;
            if (statusDefine == -2)
            {
                sql = "update WorkflowStatuses set StatusDefine=@StatusDefine,CompleteTime=getdate()";
            }
            else
            {
                sql = "update WorkflowStatuses set StatusDefine=@StatusDefine";
            }

            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@StatusDefine",statusDefine),
                new SqlParameter("@WorkParentId",workflowParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 更新为已读
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public int UpdateWorkflowChildRead(int workChildId)
        {
            string sql = "update WorkflowChildStatuses set IsRead=@IsRead";
            sql += " where WorkChildId=@WorkChildId";
            SqlParameter[] param = new SqlParameter[]
            {
                 new SqlParameter("@IsRead", 1),
                 new SqlParameter("@WorkChildId",workChildId)
            };
            return SQLHelper.Update(sql, param);
        }
        #region 退回数据跟新
        public int UpdateParentReturnReason(int workParentId, string returnReason)
        {
            string sql = "update WorkflowStatuses set ParentReturnReason=@ParentReturnReason";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@ParentReturnReason",returnReason),
                new SqlParameter("@WorkParentId",workParentId)
            };
            return SQLHelper.Update(sql, param);

        }
        #endregion
        #endregion

        #region 更新子表数据
        /// <summary>
        /// 子表下一节点更新用户名和状态
        /// 需要参数：主表ID 主表节点（状态） 子表节点
        /// 更新参数：用户名 状态激活（更改时间）
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="nodeNo"></param>
        /// <param name="childNodeNo"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int UpdateWorkflowChildStatus(string userName, WorkflowChildStatus objWorkflowChild)
        {
            string sql = "update WorkflowChildStatuses set UserName=@UserName,StatusNo=@StatusNo,LastChangeTime=@LastChangeTime where ";
            sql += "WorkParentId=@WorkParentId and NodeNo=@NodeNo and ChildNodeNo=@ChildNodeNo";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserName",userName),
                new SqlParameter("@StatusNo",objWorkflowChild.StatusNo),
                new SqlParameter("@WorkParentId",objWorkflowChild.WorkParentId),
                new SqlParameter("@NodeNo",objWorkflowChild.NodeNo),
                new SqlParameter("@ChildNodeNo",objWorkflowChild.ChildNodeNo),
                new SqlParameter("@LastChangeTime",DateTime.Now)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 子表下一节点只更新用户名
        /// 需要参数：主表ID 主表节点（状态） 子表节点
        /// 更新参数：用户名（更改时间）
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="nodeNo"></param>
        /// <param name="childNodeNo"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int UpdateWorkflowChildStatusUserName(string userName, WorkflowChildStatus objWorkflowChild)
        {
            string sql = "update WorkflowChildStatuses set UserName=@UserName,LastChangeTime=@LastChangeTime where ";
            sql += "WorkParentId=@WorkParentId and NodeNo=@NodeNo and ChildNodeNo=@ChildNodeNo";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserName",userName),
                new SqlParameter("@WorkParentId",objWorkflowChild.WorkParentId),
                new SqlParameter("@NodeNo",objWorkflowChild.NodeNo),
                new SqlParameter("@ChildNodeNo",objWorkflowChild.ChildNodeNo),
                new SqlParameter("@LastChangeTime",DateTime.Now)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 子表本节点更改
        /// 需要参数：主表id 主表节点（状态） 子表序号
        /// 更新参数：子表状态（更改时间）
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="nodeNo"></param>
        /// <param name="childNodeNo"></param>
        /// <returns></returns>
        public int UpdateWorkflowChildStatus(WorkflowChildStatus objWorkflowChild)
        {
            string sql = "update WorkflowChildStatuses set StatusNo=@StatusNo,LastChangeTime=@LastChangeTime where ";
            sql += "WorkParentId=@WorkParentId and NodeNo=@NodeNo and ChildNodeNo=@ChildNodeNo";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@StatusNo",objWorkflowChild.StatusNo),
                new SqlParameter("@WorkParentId",objWorkflowChild.WorkParentId),
                new SqlParameter("@NodeNo",objWorkflowChild.NodeNo),
                new SqlParameter("@ChildNodeNo",objWorkflowChild.ChildNodeNo),
                new SqlParameter("@LastChangeTime",DateTime.Now)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 子表下一节点批量更改
        /// 需要参数：主表id 主表节点（状态）
        /// 更新参数：子表状态（更改时间）
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="nodeNo"></param>
        /// <param name="childNodeNo"></param>
        /// <returns></returns>
        public int UpdateAllWorkflowChildStatus(WorkflowChildStatus objWorkflowChild)
        {
            string sql = "update WorkflowChildStatuses set StatusNo=@StatusNo,LastChangeTime=@LastChangeTime where ";
            sql += "WorkParentId=@WorkParentId and NodeNo=@NodeNo";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@StatusNo",objWorkflowChild.StatusNo),
                new SqlParameter("@WorkParentId",objWorkflowChild.WorkParentId),
                new SqlParameter("@NodeNo",objWorkflowChild.NodeNo),
                new SqlParameter("@LastChangeTime",DateTime.Now)
            };
            return SQLHelper.Update(sql, param);
        }
        /// <summary>
        /// 子表本节点更改
        /// 需要参数：子表id
        /// 更新参数：子表状态（更改时间）
        /// </summary>
        /// <param name="statusNo"></param>
        /// <param name="workChildId"></param>
        /// <returns></returns>
        public int UpdateWorkflowChildStatus(int statusNo, int workChildId)
        {
            string sql = "update WorkflowChildStatuses set StatusNo=@StatusNo,LastChangeTime=@LastChangeTime where ";
            sql += "WorkChildId=@WorkChildId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@StatusNo",statusNo),
                new SqlParameter("@WorkChildId",workChildId),
                new SqlParameter("@LastChangeTime",DateTime.Now)
            };
            return SQLHelper.Update(sql, param);
        }
        #region 退回数据跟新
        public int UpdateReturnReason(int workChildId, string returnReason)
        {
            string sql = "update WorkflowChildStatuses set ReturnReason=@ReturnReason";
            sql += " where WorkChildId=@WorkChildId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@ReturnReason",returnReason),
                new SqlParameter("@WorkChildId",workChildId)
            };
            return SQLHelper.Update(sql, param);

        }
        #endregion

        #endregion

        #region 日志
        /// <summary>
        /// 新增一条工作日志
        /// </summary>
        /// <param name="workflow"></param>
        /// <param name="userName"></param>
        /// <param name="type1"></param>
        /// <returns></returns>
        public int InsertWorkflowLog(int workParentId,int workflowChild, string displayName, string type1,string reason)
        {
            string sql = "insert into WorkflowLog(DisplayName,WorkParentId,WorkChildId,";
            sql += "Type1,Reason) values(@DisplayName,@WorkParentId,";
            sql += "@WorkChildId,@Type1,@Reason)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@DisplayName",displayName),
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@WorkChildId",workflowChild),
                new SqlParameter("@Type1",type1),
                new SqlParameter("@Reason",reason)
            };
            return SQLHelper.Update(sql, param);
        }


        #endregion



    }
}
